/**
 * Created by kurry on 2017/8/7.
 */
import * as favouriteServices from '../../services/favourite'
import {message} from 'antd';
import {sortBy} from '../../utils/sortArray'
export default {
  namespace: 'favorite',
  state: {
    randomClass: []
  },
  reducers: {
    //保存随机获取到的推荐视频
    saveRandomClass(state, {payload}){
      // console.log(payload);
      return {
        ...state,
        randomClass:payload,
      }
    },
  },

  effects: {
    //获取随机视频
    //获取一个tab有关的所有课程
    *fetchRandomClass({payload:values}, {call, put}){
      // console.log(values);
      let {data} = yield call(favouriteServices.fetchRandomClass,values);
      // console.log('获取到的一个组别的所有系列列表', data);
      let newData = data.map((item) => {
        item.name = item.courseClass.className;
        item.classPicName = item.courseClass.classPicName;
        // delete item.courseClass;
        delete item.course;
        return item
      });
      newData.sort(sortBy('pri', false, parseInt));
      // console.log(newData);
      yield put({type: "saveRandomClass", payload: newData})
    },
  },

  subscriptions: {
    //监听到video地址下时,获取随机推荐的组别、系列
    setup({dispatch, history}){
      return history.listen(({pathname, query}) => {
        // console.log(query);
        if (pathname === '/') {
          dispatch({type: 'fetchRandomClass', payload: query})
        }
      })
    }
  },
};
